# -*- coding:utf-8 -*-
import os
import re
from everyUnitHandler.myUtil import domainSuffix
from everyUnitHandler.qq_2 import qqExtract
from everyUnitHandler.address_2 import addressExtract
from everyUnitHandler.email_2 import emailExtract
from everyUnitHandler.mobilphone_2 import mobelphoneNumberExtract
from everyUnitHandler.telphone_2 import IncludeSwitchNum,telphoneNumExtract,quhaoInfo
from everyUnitHandler.url_2 import urlExtract
from everyUnitHandler.weichat_2 import weichatExtract
from demo.config import domainFile
from corpusPickleHandler import readPickle

# domain=readPickle(domainFile)
domain=domainSuffix.split(u'|')

def qq(workdir,fileList,filenameMd5):
    target=filenameMd5+'qq.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        qqres=[]
        for line in res:
            if line.startswith("lso") or line.startswith("iso"):
                continue
            temp=line.strip()
            if len(temp)!=0 and isNotICP(temp):
                qqres+=qqExtract(temp)
            else:
                continue
        return qqres
    else:
        return []

def address(workdir,fileList,filenameMd5):
    target=filenameMd5+'address.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        addressres=[]
        for line in res:
            line=line.strip()
            if len(line)!=0:
                line=line.replace(u'4川',u'四川').replace(u'3明',u'三明').replace(u'3门峡',u'三门峡').replace(u'3亚',u'三亚').replace(u'3沙',u'三沙').replace(u'4平',u'四平').replace(u'6安',u'六安').replace(u'6盘水',u'六盘水').replace(u'7台河',u'七台河').replace(u'9江',u'九江').replace(u'2连浩特',u'二连浩特').replace(u'2道',u'二道').replace(u'3门',u'三门')
                line=line.replace(u'3山',u'三山').replace(u'3元',u'三元').replace(u'3水',u'三水').replace(u'3穗',u'三穗').replace(u'3都',u'三都').replace(u'3原',u'三原').replace(u'3河',u'三河').replace(u'4会',u'四会').replace(u'4子王',u'四子王').replace(u'4方台',u'四方台').replace(u'5华',u'五华').replace(u'5家渠',u'五家渠').replace(u'5台',u'五台')
                line=line.replace(u'5寨',u'五寨').replace(u'5原',u'五原').replace(u'5常',u'五常').replace(u'5营',u'五营').replace(u'5大连池',u'五大连池').replace(u'5河',u'五河').replace(u'5莲',u'五莲').replace(u'5峰',u'五峰').replace(u'6枝特',u'六枝特').replace(u'6合',u'六合').replace(u'7星关',u'七星关').replace(u'7里河',u'七里河').replace(u'8宿',u'八宿')
                line=line.replace(u'8公山',u'八公山').replace(u'8步',u'八步').replace(u'9原',u'九原').replace(u'9台',u'九台').replace(u'9江',u'九江').replace(u'9龙坡',u'九龙坡').replace(u'9寨沟',u'九寨沟').replace(u'9龙',u'九龙')

                temp=addressExtract(line)

                if temp != None:

                    addressres.append(temp)
            else:
                continue
        return addressres
    else:
        return []

def email(workdir,fileList,filenameMd5):
    target=filenameMd5+'email.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        emailres=[]
        for line in res:

            if '@' in line:

                # 由于需要处理中文域名的数据，所以处理对象需要转换成unicode编码
                emailres+=emailExtract(unicode(line))
            else:
                continue
        temp=[]
        for each in emailres:
            # print "--------------------"
            # print line
            # print "--------------------"
            for onedomain in domain:
                if each.endswith(onedomain.strip()):
                    temp.append(each)
                else:
                    continue
        return temp
    else:
        return []

def mobilphone(workdir,fileList,filenameMd5):
    target=filenameMd5+'phone.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        phone=[]

        tempFlag=u''
        for line in res:
            temp=line.strip().lstrip('86')
            temp=temp.replace("-",'').replace("-",'')
            # print "---------------------"
            # print temp
            # print "---------------------"
            # 对跨行进行处理
            # if tempFlag==u'' and temp.isdigit() and temp.startswith('1') and (len(temp)==3 or len(temp)==4):
            phone+=mobelphoneNumberExtract(temp)
            if temp.isdigit() or "/" in temp or "-" in temp:

                if tempFlag==u'' and temp.isdigit() and temp.startswith('1') and len(temp)<=5 and not temp.startswith('10'):
                    tempFlag+=temp
                    continue
                if tempFlag !=u'' and temp.isdigit() and len(tempFlag)<=5 and len(temp)<=11-len(tempFlag):
                    tempFlag+=temp
                    continue
                if tempFlag !=u'' and temp.isdigit() and len(temp)<=11-len(tempFlag):
                    tempFlag+=temp
                    temp=tempFlag
                    tempFlag=u''

                if len(temp)!=0 and isNotICP(temp):
                    # print "------------------"
                    # print temp
                    # print "------------------"
                    phone+=mobelphoneNumberExtract(temp)
                else:
                    continue
            else:
                continue
        return phone
    else:
        return []

def telphone(workdir,fileList,filenameMd5):
    target=filenameMd5+'telphone.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        telphoneres=[]
        mobilphoneres=[]

        tempFlag=u''
        tempQuhaoHaoma=u''
        tempUnionQuhaoHaoma=u''

        tempHaoma=u''

        targetList=[]
        for line in res:
            if hasNumbers(line):
                line=line.strip().strip('-').strip('-')
                line=line.strip().replace("886-",'').replace("0086-",'').replace("086-",'').replace("86-",'')
                # print "--------------"
                # print line
                # print "--------------"
                if '.' in line:
                    linetemp=line.strip().split('.')
                    res+=linetemp
                    continue
                if len(line.strip())==11 and line.startswith('1'):
                    targetList.append(line)
                if line.startswith('400') or line.startswith('800'):
                    line=line.replace('-','')
                    targetList.append(line)
                    continue
                #添加跨行处理流程
                # ------------------------------------------
                # line=line.strip().replace("886-",'').replace("0086-",'').replace("086-",'').replace("86-",'')
                line=re.sub(u'-+',"-",line)
                if line.startswith("86") and len(line)>8:
                    line=line.lstrip("86")
                # print "---------------"
                # print line
                # print "---------------"
                if '-' in line and line.count('-')==2:
                    one=line.find("-")
                    two=line.rfind("-")

                    if two-one < 7:
                        splittemp=line.rsplit('-',1)
                        line=''.join(splittemp)

                if '-' in line and not line.startswith('0'):
                    if line.startswith('13') or line.startswith('14') or line.startswith('15') or line.startswith('18'):
                        line=line.replace('-','')
                    elif line.index('-') >4:
                        line=line
                    else:
                        line='0'+line
                if "-" not in line and len(line)>8 and not line.startswith('1') and not line.startswith('0'):
                    line='0'+line
                # print "----------------"
                # print line
                # print "----------------"
                if len(line)<7:
                    for key in quhaoInfo.keys():
                        if key in line:
                            tempFlag=line
                            break
                        else:
                            continue
                    if tempFlag!=u'':
                        continue

                # print "================"
                # print tempFlag
                # print "================"

                if tempFlag!=u'' and (len(line)==7 or len(line)==8) and '-' not in line:
                    line=tempFlag+"*"+line
                    tempFlag=u''
                    tempQuhaoHaoma=line

                if tempQuhaoHaoma !=u'' and line.isdigit() and len(line)<5 and line !='86':
                    line=tempQuhaoHaoma+"*"+line
                    tempQuhaoHaoma=u''
                else:
                    if tempQuhaoHaoma !=u'':
                        line=tempQuhaoHaoma

                # print "===================="
                # print line
                # print "===================="

                if "-" in line:
                    flagNum=line.count('-')
                    if flagNum==1:
                        tmp=line.split('-')
                        if tmp[0].isdigit() and tmp[-1].isdigit() and len(tmp[0])<=4:
                            # print "------1--------"
                            # print line
                            # print "------1--------"
                            tempUnionQuhaoHaoma=line
                            targetList.append(tempUnionQuhaoHaoma)
                            continue
                    elif flagNum==2:
                        targetList.append(line)

                if tempUnionQuhaoHaoma != u'' and line.isdigit() and len(line)<5 and line !='86':
                    line=tempUnionQuhaoHaoma+"-"+line
                    tempUnionQuhaoHaoma=u''
                else:
                    if tempUnionQuhaoHaoma != u'':
                        # line=tempUnionQuhaoHaoma
                        tempUnionQuhaoHaoma=u''

                if line.isdigit() and (len(line)==7 or len(line)==8):
                    tempHaoma=line
                if tempHaoma != u'' and line.isdigit() and len(line)<5 and line !='86':
                    line=tempHaoma+"-"+line
                    tempHaoma=u''
                else:
                    if tempHaoma != u'':
                        line=tempHaoma

                line=line.replace("*","-").rstrip("-")
                # print "==========2=========="
                # print line
                # print "==========2=========="

                targetList.append(line)
            else:
                continue
            # ------------------------------------------
        # 分开需要进行单独处理
        for line in targetList:
            # print "-------------------"
            # print line
            # print "-------------------"
            if len(line.strip())!=0 and isNotICP(line.strip()):
                line1=IncludeSwitchNum(line.strip())
                temp=mobelphoneNumberExtract(line1)
                # if (line1.startswith('1') and line1.isdigit() and len(line1)==11):
                #     mobilphoneres.append(line1)

                # elif len(temp)!=0:
                if len(temp)!=0:
                    mobilphoneres+=temp
                else:
                    # print "********************"
                    # print line1
                    # print "********************"
                    # 按照最新流程处理座机号
                    if len(telphoneres)!=0:
                        zhongjian=telphoneNumExtract(line1)
                        if len(zhongjian)!=0:
                            if telphoneres[-1] in zhongjian[0]:
                                del telphoneres[-1]
                                telphoneres+=zhongjian
                            else:
                                telphoneres+=zhongjian
                    else:
                        if line1.startswith('1'):
                            continue
                        telphoneres+=telphoneNumExtract(line1)  #完成对国家码的处理
            else:
                continue

        #返回前需要对座机号进行统一处理
        quhaoFlag=None
        resultTelphone=[]

        # print "----------------------"
        # print telphoneres
        # print "----------------------"

        # 是对识别结果进行二次处理
        for item in telphoneres:
            if '-' in item:
                linshi=item.split('-')
                if len(linshi[0])<=4:
                    for key,value in quhaoInfo.items():
                        if linshi[0].startswith(key) and linshi[0] in value and (len(linshi[1])==7 or len(linshi[1])==8):
                            quhaoFlag=linshi[0]
                            resultTelphone.append(item)
                            break
                else:
                    if quhaoFlag:
                        resultTelphone.append(quhaoFlag+"-"+item)
                    else:
                        resultTelphone.append(item)
            elif item.startswith('400') or item.startswith('800'):
                resultTelphone.append(item)
            elif (len(item)==7 or len(item)==8) and quhaoFlag !=None:
                resultTelphone.append(quhaoFlag+"-"+item)
            elif len(item)==4:
                for key,value in quhaoInfo.items():
                    if key in item and item in value:
                        quhaoFlag=item
            elif (len(item)==11 or len(item)==12) and item.startswith("0"):
                for key,value in quhaoInfo.items():
                    if key in item:
                        for oneV in value:
                            if item.startswith(oneV):
                            # if oneV in item:
                                quhaoFlag=oneV
                                num=item[len(quhaoFlag):]
                                if len(num)==7 or len(num)==8:
                                    resultTelphone.append(quhaoFlag+"-"+num)
                                break
                            continue
                        continue
            elif (len(item)==10 or len(item)==11) and not item.startswith("0"):
                for key,value in quhaoInfo.items():
                    # if key.lstrip("0") in item:
                    if item.startswith(key.lstrip("0")):
                        for oneV in value:
                            guodu=oneV.lstrip("0")
                            if item.startswith(guodu):
                            # if oneV in item:
                                quhaoFlag=oneV
                                num=item[len(guodu):]
                                if len(num)==7 or len(num)==8:
                                    resultTelphone.append(quhaoFlag+"-"+num)
                                break
                            continue
                        continue
            elif (len(item)==7 or len(item)==8) and quhaoFlag ==None:
                resultTelphone.append(item)
            elif len(item)>=10 and len(item)<=12 and "-" not in item and not item.startswith("1") and quhaoFlag==None and item.startswith("0"):
                for key,value in quhaoInfo.items():
                    if item.startswith(key):
                        for oneV in value:
                            if item.startswith(oneV):
                                quhaoFlag=oneV
                                num=item[len(oneV):]
                                if len(num)==7 or len(num)==8:
                                    resultTelphone.append(quhaoFlag+"-"+num)
                                break
                            continue
                        continue
                # if item[2]=='0':
                #     resultTelphone.append(item[:3]+"-"+item[3:])
                # else:
                #     resultTelphone.append(item[:4]+"-"+item[4:])
            else:
                continue
        # print "------1-------"
        # print resultTelphone
        # print "------1-------"
        return resultTelphone,mobilphoneres
    else:
        return [],[]


def url(workdir,fileList,filenameMd5):

    target=filenameMd5+'url.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        urlres=[]
        for line in res:
            if ("http" in line or 'www.' in line or "https" in line) and len(line.strip())>5:
                urlres+=urlExtract(line)
            else:
                continue
        # print "------------------"
        # print urlres
        # print "------------------"
        urlres=splitUrl(urlres)
        return urlres
    else:
        return []

def weichat(workdir,fileList,filenameMd5):
    target=filenameMd5+'weichat.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        weichatres=[]

        result=[]
        stopFlag=0
        for line in res:
            line=line.strip()
            if stopFlag==1:
                if len(line)>3:
                    weichatres.append(line)
                else:
                    continue

            if len(line.strip())!=0 and isNotICP(line.strip()) and u'微信' in line:
                # weichatres+=weichatExtract(unicode(line.strip()))
                temp=weichatExtract(unicode(line.strip()))
                if len(temp)>0:
                    weichatres+=temp
                    break
                else:
                    stopFlag=1
                    continue
            else:
                continue
        # 对提取的结果进行处理
        for each in weichatres:
            item=each.strip()
            if len(item)>=3:
                item=item.replace(':','')
                result.append(item)
                return result
        return []
    else:
        return []

# 对特例模块进行处理
def otherHandle(workdir,fileList,filenameMd5):
    target=filenameMd5+'special.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()
        file.close()
        result={'email':[],'address':[],'mobilphone':[],'telphone':[],'url':[]}

        telphone=[]
        for line in res:
            each=line.strip()
            if len(each)!=0 and isNotICP(each):
                if each.count('-')==2 and (each.startswith('400') or each.startswith('800')):
                    each=each.replace('-','')
                address=addressExtract(each)    #存储的是字符串类型
                if address !=None:
                    result['address'].append(address)

                url=urlExtract(each)       #存储的是列表类型
                result['url']+=url

                email=emailExtract(each)    #存储的是列表类型
                result['email']+=email
                mobilphone=mobelphoneNumberExtract(line)
                result['mobilphone']+=mobilphone

                line1=IncludeSwitchNum(each)
                if line1.startswith('1') and line1.isdigit() and len(line1)==11:
                    mobilphone.append(line1)
                    result['mobilphone']+=mobilphone
                elif "-" in line1 and line1.startswith('0'):
                    # print "-------------------"
                    # print line1
                    # print "-------------------"
                    telphone=telphoneNumExtract(line1)
                elif line1.isdigit() and not line1.startswith('1') and not line1.startswith('0'):
                    telphone=telphoneNumExtract(line1)
                else:
                    # telphone=telphoneNumExtract(line1)
                    continue

                # if address !=None:
                #     result['address'].append(address)
                # result['url']+=url
                # result['email']+=email
                # result['mobilphone']+=mobilphone
                result['telphone']+=telphone

            else:
                continue
        return result
    else:
        return {'email':[],'address':[],'mobilphone':[],'telphone':[],'url':[]}


def companyName(workdir,fileList,filenameMd5):
    target=filenameMd5+'companyName.txt'
    if target in fileList:
        targetFile=os.path.join(workdir,target)
        file=open(targetFile)
        res=file.readlines()

        temp=[]
        for each in res:
            if ":" in each:
                continue
            if "公司" in each:
                each=each.strip()
                index=each.find("公司")
                long=len("公司")
                tmp=[each[:index+long],each[index+long:]]
                if tmp[0]==tmp[-1]:
                    each="".join(set(tmp))
                else:
                    each="".join(tmp)
            temp.append(switchNum2Zhongwen(each))
        return temp
    else:
        return []


def isNotICP(string):
    if 'icp' in string or 'ICP' in string:
        return False
    else:
        return True

def switchNum2Zhongwen(each):
    return each.replace('0',u'零').replace('1',u'一').replace('2',u'二').replace('3',u'三').replace('4',u'四').replace('5',u'五').replace('6',u'六').replace('7',u'七').replace('8',u'八').replace('9',u'九')

def hasNumbers(inputString):
    # return bool(re.search(r'\d',inputString)) or inputString.endswith(('0','1','2','3','4','5','6','7','8','9'))

    inputString=inputString.strip().strip('-').strip('-')
    # print "--------------"
    # print inputString
    # print "--------------"
    return inputString.endswith(('0','1','2','3','4','5','6','7','8','9'))

def splitUrl(urlList):
    if len(urlList)==0:
        return urlList
    resUrlList=[]
    for each in urlList:
        wwwNum=each.count("www")
        if wwwNum<=1:
            resUrlList.append(each)
        else:
            temp=[m.start() for m in re.finditer('www',each)]
            for i in xrange(len(temp)):
                if i==len(temp)-1:
                    tmp=each[temp[i]:]
                    tmp=tmp.rstrip("https://").rstrip("http://")
                    resUrlList.append(tmp)
                else:
                    tmp=each[temp[i]:temp[i+1]]
                    tmp=tmp.rstrip("https://").rstrip("http://")
                    resUrlList.append(tmp)
    return resUrlList

# if __name__=='__main__':
#     tmp=['www.baidu.com','www.baidu2.comwww.baidu3.com']
#     print splitUrl(tmp)





